﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace medicine.BaseComponents.Groups
{
    public partial class SelectedGroup : UserControl,IDragSource ,IDragDestination 
    {

        public int GroupOrder { get; set; }

        public SelectedGroup()
        {
            InitializeComponent();
            OriginalColor = this.pnlDest.BackColor;
        }
        
        public bool isPointInRect(Point p, Rectangle rec)
        {
            if (p.X > rec.Left && p.X < rec.Right && p.Y > rec.Top && p.Y < rec.Bottom)
                return true;
            else
                return false;
        }

        public bool isPopulated()
        {
            return this.Controls.Count > 0;
        }

        public GroupInfo GetGroupInfo()
        {
            return ((GroupLabel)this.Controls[0]).GroupInfo;
        }

        #region IDragSource Members

        public event DraggingHandler Dragging;

        public event DropTestHandler DropTest;

        public void DragCompleted(Control Sender)
        {
            ((IDraggableControl)Sender).Mover.Dragging -= Mover_Dragging;
            ((IDraggableControl)Sender).Mover.DropTest -= Mover_DropTest;

            pnlDest.Controls.Clear();
        }

        #endregion

        #region IDragDestination Members


        public new Control DragDrop(Control DraggedLabel)
        {
            pnlDest.Controls.Clear();
            pnlDest.Controls.Add(DraggedLabel);
            DraggedLabel.Dock = DockStyle.Fill;
            ((IDraggableControl)DraggedLabel).Mover.Dragging += new DraggingHandler(Mover_Dragging);
            ((IDraggableControl)DraggedLabel).Mover.DropTest += new DropTestHandler(Mover_DropTest);
            ((GroupLabel)DraggedLabel).GroupInfo.isSelectedRow = true;
            ((GroupLabel)DraggedLabel).GroupInfo.SelectedRowOrder = this.GroupOrder;
            return pnlDest;
        }

        Control Mover_DropTest(Control Sender)
        {
            if (this.DropTest != null)
                return this.DropTest(Sender);
            return null;
        }

        void Mover_Dragging(Control Sender, Rectangle Area)
        {
            if (this.Dragging != null)
                Dragging(Sender, Area);
        }

        private Color OriginalColor;
        public void DragOver()
        {
            pnlDest.BackColor = System.Drawing.SystemColors.ControlDarkDark;

        }

        public void DragOut()
        {
            pnlDest.BackColor = OriginalColor;
        }

        public bool TestHitTarget(Control Sender, Rectangle Rec)
        {
            //if (pnlDest.Controls.Count  > 0)
            //    return false;
            if (((IDraggableControl)Sender).Mover.ParentContainer == pnlDest)
                return false;

            const int Shift=5;

            int TotalLeft = this.Left + pnlDest.Left + this.Parent.Left + this.Parent.Parent.Left ;
            int TotalTop = this.Top + pnlDest.Top + this.Parent.Top + this.Parent.Parent.Top;
            //int TotalBottm = this.Bottom + pnlDest.Top + this.Parent.Top + this.Parent.Parent.Top;
            //int TotalRight = this.Right + pnlDest.Left + this.Parent.Left + this.Parent.Parent.Left;
            Rectangle pnldestRect=new Rectangle(TotalLeft + Shift,TotalTop + Shift,pnlDest.Width -Shift ,pnlDest.Height - Shift);

            Point LeftTop = new Point(Rec.Left, Rec.Top);
            Point LeftBottom = new Point(Rec.Left, Rec.Bottom);
            Point RightTop = new Point(Rec.Right, Rec.Top);
            Point RightBottom = new Point(Rec.Right, Rec.Bottom);

            if (isPointInRect(LeftTop, pnldestRect)) return true;
            if (isPointInRect(LeftBottom, pnldestRect)) return true;
            if (isPointInRect(RightTop , pnldestRect)) return true;
            if (isPointInRect(RightBottom , pnldestRect)) return true;
            return false;
            
        }


        #endregion

 
    }
}
